package com.wn.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn.VO.LngAndLatVO;
import com.wn.common.pojo.*;
import com.wn.common.util.FindPosition;
import com.wn.mapper.DestinationMapper;
import com.wn.mapper.HotelMapper;
import com.wn.mapper.ResterauntMapper;
import com.wn.mapper.TravelRouteMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class TravelRouteService {

    @Autowired
    private DestinationMapper destinationMapper;//目的地
    @Autowired
    private ScenicSpotsService scenicSpotsService;//景点
    @Autowired
    private HotelMapper hotelMapper;//酒店
    @Autowired
    private ResterauntMapper resterauntMapper;//饮食

    @Autowired
    private TravelRouteMapper travelRouteMapper;
    /**
     * 查询所有列表
     * @param pageParam
     * @return
     */
    public Rsp queryTravelRouteList(PageParam<ScenicSpots> pageParam) {

        if (!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())) {
            //执行高级检索
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());

            List<TravelRoute> travelRoutes = travelRouteMapper.superSearch(pageParam.getSuperSearchKeyWord());

            for (TravelRoute t: travelRoutes) {
                //调用查询三处的方法
                findTraveRouteAll(t);

            }

            PageInfo<TravelRoute> ResterauntPageInfo = new PageInfo(travelRoutes);

            return Rsp.success(ResterauntPageInfo);

        } else {

            PageHelper.startPage(pageParam.getPageNum(),pageParam.getPageSize());
            List<TravelRoute> travelRoutes = travelRouteMapper.queryTravelRouteList();

            for (TravelRoute t: travelRoutes) {
                //调用查询三处的方法
                findTraveRouteAll(t);
            }
            PageInfo<TravelRoute> pageInfo = new PageInfo(travelRoutes);
            return Rsp.success(pageInfo);
        }
    }

    //修改状态
    public Rsp updateNewsinfo(TravelRoute travelRoute) {
        if (travelRoute.getStatus() == 1){
            travelRoute.setStatus(0);
        }else{
            travelRoute.setStatus(1);
        }
        int i = travelRouteMapper.updateNewsinfo(travelRoute);

        if(i == 1){
            return Rsp.success(null);
        }else{
            return Rsp.fail();
        }

    }

    /**
     * 逻辑删除
     * @param travelRoute
     */
    public Rsp deleteTravelRouteListById(TravelRoute travelRoute) {

        int i = travelRouteMapper.deleteTravelRouteListById(travelRoute);

        if (i == 1){
            return Rsp.success(null);
        }else{
            return Rsp.fail();
        }
    }



    /**
     * 根据目的地名称搜索出是否存在该目的地
     * @param routeEndPlace
     */
    public Rsp findTravelRouteByName(String routeEndPlace) {

        TravelRoute travelRouteByName = travelRouteMapper.findTravelRouteByName(routeEndPlace);

        if(StringUtils.isEmpty(travelRouteByName)){
            return Rsp.fail();
        }else{
            return Rsp.success(travelRouteByName);
        }
    }

    /**
     * 根据目的地名称搜索出该地路线
     * @param routeEndPlace
     * @return
     */
    public Rsp findTravelRouteListByName(String routeEndPlace) {

        List<TravelRoute> travelRoutes = travelRouteMapper.findTravelRouteListByName(routeEndPlace);

        System.out.println("---"+travelRoutes);
        if(travelRoutes.isEmpty() || travelRoutes.size() == 0){
            return Rsp.fail().code(1);
        }

        for (TravelRoute t: travelRoutes) {
            //调用查询三处的方法
            findTraveRouteAll(t);
        }

        return Rsp.success(travelRoutes);

    }



    /**
     * 查询出该目的地周围的 三个区域
     * @param t
     */
    public void findTraveRouteAll(TravelRoute t){
        //根据目的地名搜索目的地对象
        Destination destination = destinationMapper.selectByName(t.getRouteEndPlace());

        MaxAndMinLatAndLng neighPosition = FindPosition.findNeighPosition(destination);
        //搜索城市周围景点的业务
        List<ScenicSpots> scenicSpots = scenicSpotsService.getScenicSpotsByLatAndLng(neighPosition);
        //搜索城市周围酒店的业务
        List<Hotel> hotelByLatAndLng = hotelMapper.getHotelByLatAndLng(neighPosition);
        //搜索城市周围餐饮的业务
        List<Resteraunt> resProducts = resterauntMapper.getResProductByLatAndLng(neighPosition);

        t.setResterauntList(resProducts);
        t.setHotelList(hotelByLatAndLng);
        t.setScenicSpotsList(scenicSpots);

        List<LngAndLatVO> list = new ArrayList<>();

        for (ScenicSpots s: scenicSpots) {

            LngAndLatVO lngAndLatVO = new LngAndLatVO();
            lngAndLatVO.setLng(s.getLng());
            lngAndLatVO.setLat(s.getLat());

            list.add(lngAndLatVO);
        }

        t.setListLngAndLat(list);
        System.out.println("最后"+list);
    }

    /**
     * 添加
     * @param travelRoute
     */
    public Rsp addTravelRoute(TravelRoute travelRoute) {

        int i = travelRouteMapper.insertSelective(travelRoute);

        if(i == 1){
            return Rsp.success(i);
        }else{
            return Rsp.fail();
        }


    }

    /**
     * 修改
     * @param travelRoute
     */
    public Rsp updateTravelRoute(TravelRoute travelRoute) {

        int i = travelRouteMapper.updateByPrimaryKeySelective(travelRoute);

        if(i == 1){
            return Rsp.success(i);
        }else{
            return Rsp.fail();
        }

    }

    /**
     * g根据id
     * @param travelRoute
     */
    public Rsp findTraveRouteById(TravelRoute travelRoute) {

        TravelRoute traveRoute = travelRouteMapper.findTraveRouteById(travelRoute.getId());

            //调用查询三处的方法
            findTraveRouteAll(traveRoute);

        return Rsp.success(traveRoute);
    }
}
